﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FeaturesBlog.Models.Interfaces;

namespace FeaturesBlog.Models.Repositories
{
    public class CommentsRepository : ICommentsRepository
    {
        #region Variables
        private MyFeaturesBlogClassesDataContext _context;
        #endregion

        #region Methods
        public CommentsRepository(MyFeaturesBlogClassesDataContext c)
        {
            _context = c;
        }
        public Comment GetComment(decimal commentId)
        {
            return _context.Comments.SingleOrDefault(c => c.CommentId == commentId);
        }
        public IEnumerable<Comment> GetCommentsByPostId(decimal postId)
        {
            return _context.Comments.Where(c => c.PostId == postId).OrderBy(c => c.CommentDate);
        }
        public void AddComment(Comment c)
        {
            _context.Comments.InsertOnSubmit(c);
            _context.SubmitChanges();
        }
        public void DeleteComment(decimal commentId)
        {
            _context.Comments.DeleteOnSubmit(GetComment(commentId));
            _context.SubmitChanges();
        }
        public int GetCommentCountByPostId(decimal postId)
        {
            return _context.Comments.Count(c => c.PostId == postId);
        }
        #endregion
    }
}